package com.haoshuang.zhouzhoubang.ui.page.home.settings.rate

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityTeamRateSetBinding
import com.haoshuang.zhouzhoubang.model.bean.TeamSettingsInfoBean
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.convertInt
import com.mufeng.libs.utils.ktx.fromJson
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 *  门店比例分成设置
 */
class TeamRateSetActivity : BaseVMActivity<TeamRateSetViewModel, ActivityTeamRateSetBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<TeamRateSetActivity>()
        }
    }

    private var teamInfo: TeamSettingsInfoBean? = null

    override fun initIntent() {

    }

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "分成设置"
        binding.titleBar.leftImageView().click { finish() }

        binding.apply {
            etTeam.setOnFocusChangeListener { v, hasFocus ->
                if (hasFocus) {
                    etTeam.setSelection(etTeam.text.length)
                }
            }

            etMember.setOnFocusChangeListener { v, hasFocus ->
                if (hasFocus) {
                    etMember.setSelection(etMember.text.length)
                }
            }

            etLeader.setOnFocusChangeListener { v, hasFocus ->
                if (hasFocus) {
                    etLeader.setSelection(etLeader.text.length)
                }
            }

        }

        binding.tvCommit.clickWithTrigger {
            val team_rate = binding.etTeam.text.toString()
            val team_leader_rate = binding.etLeader.text.toString()
            val team_member_rate = binding.etMember.text.toString()
            if (team_rate.isNullOrBlank()) {
                toast("队长分成不能为空")
                return@clickWithTrigger
            }

            if (team_member_rate.isNullOrBlank()) {
                toast("队员分成不能为空")
                return@clickWithTrigger
            }

            val num = team_member_rate.toInt() + team_rate.toInt()
            if (num > 100) {
                toast("分成总和不应超过为100%")
                return@clickWithTrigger
            }
            if (num < 100) {
                toast("分成总和不应低于为100%")
                return@clickWithTrigger
            }
            if (teamInfo?.surplus_rate == "0"){
                toast("您已达到最大修改次数")
                return@clickWithTrigger
            }
            vm.updateTeamRate(
                team_rate,
                team_leader_rate,
                team_member_rate,
            )
        }
    }

    override fun initData() {
        vm.getTeamSettingsInfo()
    }

    override fun startObserve() {
        vm.getTeamSettingsInfoData.observeState(this){
            onLoading {  }
            onError { code, msg ->
            }
            onSuccess {
                teamInfo = it?.data
                binding.tvResidueDegree.text = "当前剩余${teamInfo?.surplus_rate}次修改分成次数"
                if (teamInfo?.team_rate == "0") {
                    binding.etTeam.setText("")
                }else{
                    binding.etTeam.setText(teamInfo?.team_rate.toString())
                }
                if (teamInfo?.team_leader_rate == "0") {
                    binding.etLeader.setText("")
                }else{
                    binding.etLeader.setText(teamInfo?.team_leader_rate.toString())
                }
                if (teamInfo?.team_member_rate == "0") {
                    binding.etMember.setText("")
                }else {
                    binding.etMember.setText(teamInfo?.team_member_rate.toString())
                }

                binding.etTeam.isEnabled = teamInfo?.surplus_rate.convertInt() > 0
                binding.etLeader.isEnabled = teamInfo?.surplus_rate.convertInt() > 0
                binding.etMember.isEnabled = teamInfo?.surplus_rate.convertInt() > 0


                binding.tvTeamTip.text = "门店分成比例建议设置比例：${teamInfo?.team_rate_see}"
                binding.tvLeaderTip.text = "负责人分成比例建议设置比例：${teamInfo?.team_leader_rate_see}"
                binding.tvMemberTip.text = "队员分成比例建议设置比例：${teamInfo?.team_member_rate_see}"
            }
        }
        vm.updateTeamRateData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                val team_rate = binding.etTeam.text.toString()
                val team_leader_rate = binding.etLeader.text.toString()
                val team_member_rate = binding.etMember.text.toString()
                setResult(200, Intent().apply {
                    putExtra("team_rate", team_rate)
                    putExtra("team_leader_rate", team_leader_rate)
                    putExtra("team_member_rate", team_member_rate)
                })
                finish()
            }
        }
    }

}